package DCSource;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class filer{
	private boolean isWindowsSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("windows") >= 0;
	  }
	  
	private boolean isLinuxSystem()
	  { String osName = System.getProperty("os.name").toLowerCase();
	    return osName.indexOf("linux") >= 0;
	  }
	private String umsch(){
		if (isWindowsSystem()==true){
			return String.valueOf((char)13)+String.valueOf((char)10);
		}else{return String.valueOf((char)10);}
	}
	private String file="";
	public filer(){}
	public filer(String uri){
		file=uri;
	}
	public void setFile(String uri){
		file=uri;
	}
	public void writeText(String text){
		try {
			FileOutputStream write = new FileOutputStream(file);
			write.write(text.getBytes());
			write.close();
		} catch (IOException e) {}
	}
	public void writeText(String text,String uri){
		file=uri;
		try {
			FileOutputStream write = new FileOutputStream(file);
			write.write(text.getBytes());
			write.close();
		} catch (IOException e) {}
	}
	public void writeFile(byte[] src){
		try {
			FileOutputStream write = new FileOutputStream(file);
			write.write(src);
			write.close();
		} catch (IOException e) {}
	}
	public void writeFile(byte[] src,String uri){
		file=uri;
		try {
			FileOutputStream write = new FileOutputStream(file);
			write.write(src);
			write.close();
		} catch (IOException e) {}
	}
	public boolean exist(){
		File f = new File(file);
		return f.exists();
	}
	public boolean exist(String uri){
		file=uri;
		File f = new File(file);
		return f.exists();
	}
	public String loadText(){
		try{
			   File test = new File(file);
			   FileInputStream inputstream=new FileInputStream(test);
			   BufferedInputStream bis = new BufferedInputStream(inputstream);
			   int avail = bis.available();
			   ArrayList<String> toRead = new ArrayList<String>();
			   int toReadlen = avail;
			   while (toReadlen>0){
				   if (toReadlen-1024>0){
					   toRead.add(new Integer(1024).toString());
				   }else{
					   toRead.add(new Integer(toReadlen).toString());
				   }
				   toReadlen-=1024;
			   }
			   String text = "";
			   for (int I=0;I<toRead.size();I++){
				   byte[] all = new byte[new Integer(toRead.get(I))];
				   bis.read(all,0,new Integer(toRead.get(I)));
				   text += new String(all);
			   }
			   bis.close();
			   return text;
		   }catch(Exception err){
			   return "";
		   }
	}
	public String loadText(String uri){
		file = uri;
		try{
			   File test = new File(file);
			   FileInputStream inputstream=new FileInputStream(test);
			   BufferedInputStream bis = new BufferedInputStream(inputstream);
			   int avail = bis.available();
			   ArrayList<String> toRead = new ArrayList<String>();
			   int toReadlen = avail;
			   while (toReadlen>0){
				   if (toReadlen-1024>0){
					   toRead.add(new Integer(1024).toString());
				   }else{
					   toRead.add(new Integer(toReadlen).toString());
				   }
				   toReadlen-=1024;
			   }
			   String text = "";
			   for (int I=0;I<toRead.size();I++){
				   byte[] all = new byte[new Integer(toRead.get(I))];
				   bis.read(all,0,new Integer(toRead.get(I)));
				   text += new String(all);
			   }
			   bis.close();
			   return text;
		   }catch(Exception err){
			   return "";
		   }
	}
	public byte[] loadFile(){
		try{
	      String link = file;
	      String returning="";
	      File file=new File(link);
	      FileInputStream inputstream=new FileInputStream(file);
	      BufferedInputStream bis = null;
	      bis = new BufferedInputStream(inputstream);
	      byte[] buffer = null;
	      int avail = bis.available();
	      if ( avail>0 )
	      {
	    	  buffer = new byte[avail];
	    	  bis.read(buffer, 0, avail);
	      }
	      bis.close();
	      return buffer;
		}catch(Exception err){return null;}
	}
	public byte[] loadFile(String uri){
		file=uri;
		try{
	      String link = file;
	      String returning="";
	      File file=new File(link);
	      FileInputStream inputstream=new FileInputStream(file);
	      BufferedInputStream bis = null;
	      bis = new BufferedInputStream(inputstream);
	      byte[] buffer = null;
	      int avail = bis.available();
	      if ( avail>0 )
	      {
	    	  buffer = new byte[avail];
	    	  bis.read(buffer, 0, avail);
	      }
	      bis.close();
	      return buffer;
		}catch(Exception err){return null;}
	}
	public ArrayList<byte[]> loadFilePro(){
		try{
			   File test = new File(file);
			   FileInputStream inputstream=new FileInputStream(test);
			   BufferedInputStream bis = new BufferedInputStream(inputstream);
			   int avail = bis.available();
			   ArrayList<String> toRead = new ArrayList<String>();
			   int toReadlen = avail;
			   while (toReadlen>0){
				   if (toReadlen-1024>0){
					   toRead.add(new Integer(1024).toString());
				   }else{
					   toRead.add(new Integer(toReadlen).toString());
				   }
				   toReadlen-=1024;
			   }
			   ArrayList<byte[]> src = new ArrayList<byte[]>();
			   for (int I=0;I<toRead.size();I++){
				   byte[] all = new byte[new Integer(toRead.get(I))];
				   bis.read(all,0,new Integer(toRead.get(I)));
				   src.add(all);
			   }
			   bis.close();
			   return src;
		   }catch(Exception err){
			   return new ArrayList();
		   }
	}
	public ArrayList<byte[]> loadFilePro(String uri){
		file = uri;
		try{
			   File test = new File(file);
			   FileInputStream inputstream=new FileInputStream(test);
			   BufferedInputStream bis = new BufferedInputStream(inputstream);
			   int avail = bis.available();
			   ArrayList<String> toRead = new ArrayList<String>();
			   int toReadlen = avail;
			   while (toReadlen>0){
				   if (toReadlen-1024>0){
					   toRead.add(new Integer(1024).toString());
				   }else{
					   toRead.add(new Integer(toReadlen).toString());
				   }
				   toReadlen-=1024;
			   }
			   ArrayList<byte[]> src = new ArrayList<byte[]>();
			   for (int I=0;I<toRead.size();I++){
				   byte[] all = new byte[new Integer(toRead.get(I))];
				   bis.read(all,0,new Integer(toRead.get(I)));
				   src.add(all);
			   }
			   bis.close();
			   return src;
		   }catch(Exception err){
			   return new ArrayList();
		   }
	}
	public void deletfile(){
		File local = new File(file);
		if (local.exists()){
			local.delete();
		}
	}
	public void deletfile(String uri){
		file=uri;
		File local = new File(file);
		if (local.exists()){
			local.delete();
		}
		
	}
}
